//
//  HXInputScreenViewController.m
//  HongxuanBluetooth
//
//  Created by 广州弘轩科技发展有限公司 on 2023/8/17.
//

#import "HXInputScreenViewController.h"
#import <AudioToolbox/AudioToolbox.h>
@interface HXInputScreenViewController ()
@property (strong,nonatomic) UISlider *slider;
@property (strong,nonatomic) UIImageView *btview;
@property (strong,nonatomic) UIImageView *borderview;//边框
@property (strong,nonatomic) UISlider *scalingslider;//缩放按钮
@property (strong,nonatomic) UILabel *bltime;//小时
@property (strong,nonatomic) UILabel *bltimetwo;//小时
@property (strong,nonatomic) UILabel *maohaolb;
@property (strong,nonatomic) UILabel *timelb;
@property (assign,nonatomic) NSInteger indexValue;//记录点击成功下标
@property (assign,nonatomic) NSInteger tenHour;//24小时制十位。tenHour
@property (assign,nonatomic) NSInteger unitHour;//24小时制个位。unitHour
@property (assign,nonatomic) NSInteger tenMinute;//24小时制十分位。tenMinute
@property (strong,nonatomic) NSString *ultimateTime; //最终的时间;
@property (strong, nonatomic) BlueToothManager *blueToothManager;//蓝牙
@property (strong,nonatomic)  NSTimer* vibrationTimer;//震动计时器

@property (strong,nonatomic) UIImageView * batterystatus;
@property (assign,nonatomic) BOOL isArrive;//是否到达

@property (strong,nonatomic) NSMutableArray *disconnectDeviceArray;//断开的的数组
@property (strong,nonatomic) NSMutableArray *linkDeviceArray;//链接的的数组

@property (strong,nonatomic) NSString *disconnectDeviceuuid; //最终uuid;
@property (strong,nonatomic)UIImageView *devicesignalview ;
@property (strong,nonatomic) NSString *finishTime;

@property (strong,nonatomic) NSString *UUID;
@end

@implementation HXInputScreenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"view加载完毕");
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor blackColor];
    self.fd_prefersNavigationBarHidden = YES;
    self.indexValue = 1;
    NSLog(@"InputScreen dataaraay :%@",self.dataaraay);
    
    NSDictionary *dict = [NSDictionary dictionaryWithDictionary:self.dataaraay];
    
    self.UUID = [NSString stringWithFormat:@"%@",dict[@"uuid"]] ;
    NSLog(@"self.UUID :%@",self.UUID);

    self.blueToothManager = [BlueToothManager shareInstance];
    [self.blueToothManager StartScan];//打开蓝牙
    
    [self BottomFunction];
    
    [self resendTime];//上次未到到对应时间，现在重新跑。
    
    [self dianliang];//获取电量
    
}
- (NSMutableArray *)disconnectDeviceArray{
    
    if(!_disconnectDeviceArray){
        _disconnectDeviceArray = [[NSMutableArray alloc] init];
        
    }
    return _disconnectDeviceArray;
}
- (NSMutableArray *)linkDeviceArray{
    
    if(!_linkDeviceArray){
        _linkDeviceArray = [[NSMutableArray alloc] init];
        
    }
    return _linkDeviceArray;
}
-(void)BottomFunction{
    
    UIImageView *tabr = [[UIImageView alloc] initWithFrame:CGRectMake(0, kNavHeight-44, winWidth, 44)];
    [self.view addSubview:tabr];
    UIImageView *mainscreen = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, winWidth, 44)];
    mainscreen.image = [UIImage imageNamed:@"helpermodeba"];
    [tabr addSubview:mainscreen];
    
    //    scaling 缩放
    UIImageView *scalingview = [[UIImageView alloc] init];
    scalingview.image = [UIImage imageNamed:@"scaling"];
    scalingview.userInteractionEnabled = YES;
    [self.view addSubview:scalingview];
    [scalingview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.mas_equalTo(tabr.mas_bottom).offset(31);
        make.width.height.mas_equalTo(31);
    }];
    
    UISlider *scalingslider = [[UISlider alloc] init];
    scalingslider.minimumValue = 0.0;
//    获取当前屏幕的亮度，赋值在滑块上
//    CGFloat valuea = [UIScreen mainScreen].brightness;
    //03.当前值
    NSString *sliderarr= [[NSUserDefaults standardUserDefaults] objectForKey:@"luminance"];
    double num = [sliderarr doubleValue];
    
    if(winHeight<=736){
        if(num>0){
            scalingslider.value = num;
        }else{
            NSString *sliderarr= [NSString stringWithFormat:@"1"];
            [[NSUserDefaults standardUserDefaults] setObject:sliderarr forKey:@"luminance"];
              //这里建议同步存储到磁盘中，但是不是必须的
            [[NSUserDefaults standardUserDefaults] synchronize];
            scalingslider.value = 1;
        }
    }else{
        if(num>0){
            scalingslider.value = num;
        }else{
            NSString *sliderarr= [NSString stringWithFormat:@"0.5"];
            [[NSUserDefaults standardUserDefaults] setObject:sliderarr forKey:@"luminance"];
              //这里建议同步存储到磁盘中，但是不是必须的
            [[NSUserDefaults standardUserDefaults] synchronize];
            scalingslider.value = 0.5;
        }
    }
    
//    if(num>0){
//        scalingslider.value = num;
//    }else{
//        NSString *sliderarr= [NSString stringWithFormat:@"0.5"];
//        [[NSUserDefaults standardUserDefaults] setObject:sliderarr forKey:@"luminance"];
//          //这里建议同步存储到磁盘中，但是不是必须的
//        [[NSUserDefaults standardUserDefaults] synchronize];
//        scalingslider.value = 0.5;
//    }
//    scalingslider.value = 0.5;
    [scalingslider setMinimumTrackImage:[UIImage imageNamed:@"xuanzhong"] forState:UIControlStateNormal];
    [scalingslider setMaximumTrackImage:[UIImage imageNamed:@"beijing"] forState:UIControlStateNormal];
    //    生成透明图片
    CGSize sa=CGSizeMake(0.1, 0.1);
    UIGraphicsBeginImageContextWithOptions(sa, 0, [UIScreen mainScreen].scale);
    UIRectFill(CGRectMake(0, 0, 0, 0));
    UIImage *imga=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //    设置按钮拇指图片
    //    添加图片，大按钮就去掉了，这个img置为空是去不掉的
    [scalingslider setThumbImage:imga forState:UIControlStateNormal];
    scalingslider.transform = CGAffineTransformMakeRotation( 3 * M_PI_2);
    [scalingslider setContinuous:YES];
    scalingslider.hidden = YES;
    self.scalingslider = scalingslider;
    [self.view addSubview:scalingslider];
    [scalingslider addTarget:self action:@selector(scalingslider:) forControlEvents:UIControlEventValueChanged];
    [scalingslider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(-45);
        make.top.mas_equalTo(scalingview.mas_bottom).offset(70);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(148);
    }];
    
    
    UIButton *scalingbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [scalingview addSubview:scalingbt];
    [scalingbt addTarget:self action:@selector(scalingbt) forControlEvents:UIControlEventTouchUpInside];
    [scalingbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(scalingview);
    }];
    
    
    //    devicesignal 信号
    UIImageView *devicesignalview = [[UIImageView alloc] init];
    devicesignalview.image = [UIImage imageNamed:@"devicesignal"];
    self.devicesignalview = devicesignalview;
    [self.view addSubview:devicesignalview];
    [devicesignalview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(scalingview.mas_right).offset(28);
        make.top.mas_equalTo(tabr.mas_bottom).offset(36);
        make.height.mas_equalTo(24);
        make.width.mas_equalTo(49);
    }];

//    暂时定位信号值
    WeakSelf
    static int dian = 0;
    self.blueToothManager.RSSIBlock = ^(NSInteger rsii) {
        dian ++;
        if(rsii>=-50){
//            NSLog(@"信号0 -- -50");
            devicesignalview.image = [UIImage imageNamed:@"signalmian"];
        }
        if(rsii< -50&&rsii>=-60){
//            NSLog(@"信号-50 -- -60");
            devicesignalview.image = [UIImage imageNamed:@"signalmian3"];
        }
        if(rsii< -60&&rsii>-70){
//            NSLog(@"信号-60 -- -70");
            devicesignalview.image = [UIImage imageNamed:@"signalmian2"];
        }
        if(rsii<= -70&&rsii>-80){
//            NSLog(@"信号-60 -- -70");
            devicesignalview.image = [UIImage imageNamed:@"signalmian1"];
        }
        if(rsii<= -80){
//            NSLog(@"信号-80以上");
            devicesignalview.image = [UIImage imageNamed:@"signalmian0"];
        }
//        timelb.text = [NSString stringWithFormat:@"%ld",rsii];
        if(dian % 60 == 0){
            NSLog(@"第一分钟查询电量");
            [weakSelf dianliang];
        }
    };
    
    
    self.blueToothManager.datalasttimeBlock = ^(NSString * _Nonnull lasttime) {
        NSLog(@"获取设备返回来数据:%@",lasttime);
        NSArray *Arr = [NSArray array];
        Arr = [lasttime componentsSeparatedByString:@"0x585703"];
//        NSLog(@"获取最后的时间：%@",Arr);
        NSString *time = [Arr[1] substringWithRange:NSMakeRange(8, 2)];
        NSLog(@"time ：%@",time);
        if([time isEqualToString:@"02"]){
            weakSelf.isArrive = YES;//发送时no 到指定位置折为yes
            [weakSelf stopvibration];//暂停震动
        }
        
    };
    
    
    
    UILabel *timelb = [[UILabel alloc] init];
    if(self.bluetoothTime.length>0){
        timelb.text = self.bluetoothTime;
    }else{
        timelb.text = @"00:00";
    }
//    timelb.text = @"10:00";
    timelb.textColor = [UIColor whiteColor];
    timelb.font = [UIFont systemFontOfSize:24];
    self.timelb = timelb;
    [self.view addSubview:timelb];
    [timelb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(tabr.mas_bottom).offset(43);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(19);
    }];
    
    
    
    
    
    //    luminance 亮度
    UIImageView *luminanceview = [[UIImageView alloc] init];
    luminanceview.image = [UIImage imageNamed:@"luminance"];
    luminanceview.userInteractionEnabled = YES;
    [self.view addSubview:luminanceview];
    [luminanceview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-12);
        make.top.mas_equalTo(tabr.mas_bottom).offset(31);
        make.width.mas_equalTo(31);
        make.height.mas_equalTo(33);
    }];
    
    UIButton *luminancebt = [UIButton buttonWithType:UIButtonTypeCustom];
    [luminanceview addSubview:luminancebt];
    [luminancebt addTarget:self action:@selector(luminancebt) forControlEvents:UIControlEventTouchUpInside];
    [luminancebt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(luminanceview);
    }];
    
    
    //    electricquantity 电量
    UIImageView *electricquantityview = [[UIImageView alloc] init];
    electricquantityview.image = [UIImage imageNamed:@"electricquantity"];
    self.batterystatus = electricquantityview;
    [self.view addSubview:electricquantityview];
    [electricquantityview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(luminanceview.mas_left).offset((-28-44));
        make.top.mas_equalTo(tabr.mas_bottom).offset(36);
        make.height.mas_equalTo(24);
        make.width.mas_equalTo(44);
    }];
    
    
    //    border  边框
    UIImageView *borderview = [[UIImageView alloc] init];
    borderview.image = [UIImage imageNamed:@"border"];
    self.borderview = borderview;
    [self.view addSubview:borderview];
    [borderview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(timelb.mas_bottom).offset(10);
        make.centerX.mas_equalTo(self.view);
        make.left.mas_equalTo(45);
        make.right.mas_equalTo(-45);
        //        make.height.mas_equalTo(136);
        //        make.width.mas_equalTo(282);
    }];
    
    //    时间剧中
    UILabel *bltime = [[UILabel alloc] init];
    bltime.text = @"  ";
    bltime.textColor = [UIColor whiteColor];
    bltime.textAlignment = NSTextAlignmentCenter;
    self.bltime = bltime;
    //    bltime.font = [UIFont systemFontOfSize:85];
    bltime.font = [UIFont boldSystemFontOfSize:80];
    [borderview addSubview:bltime];
    [bltime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(19);
        make.width.mas_equalTo((141-38));
        make.top.mas_equalTo(34);
        make.bottom.mas_equalTo(-38);
    }];
    
    
    UILabel *maohaolb = [[UILabel alloc] init];
    maohaolb.text = @":";
    maohaolb.textColor = [UIColor whiteColor];
    maohaolb.font = [UIFont boldSystemFontOfSize:85];
    self.maohaolb = maohaolb;
    [borderview addSubview:maohaolb];
    [maohaolb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-38);
        make.centerX.mas_equalTo(self.view);
        make.height.mas_equalTo(68);
        
    }];
    
    UILabel *bltimetwo = [[UILabel alloc] init];
    bltimetwo.text = @"  ";
    bltimetwo.textColor = [UIColor whiteColor];
    bltimetwo.textAlignment = NSTextAlignmentCenter;
    //    bltime.font = [UIFont systemFontOfSize:85];
    self.bltimetwo = bltimetwo;
    bltimetwo.font = [UIFont boldSystemFontOfSize:80];
    [borderview addSubview:bltimetwo];
    [bltimetwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-19);
        make.width.mas_equalTo((141-38));
        make.top.mas_equalTo(34);
        make.bottom.mas_equalTo(-38);
    }];
    //    underline 下划线
    
    UIImageView *underline = [[UIImageView alloc] init];
    underline.backgroundColor = [UIColor whiteColor];
    [borderview addSubview:underline];
    [underline mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(19);
        make.width.mas_equalTo((141-38));
        make.top.mas_equalTo(bltime.mas_bottom).offset(10);
        make.height.mas_equalTo(2.3);
    }];
    UIImageView *underlinetwo = [[UIImageView alloc] init];
    underlinetwo.backgroundColor = [UIColor whiteColor];
    [borderview addSubview:underlinetwo];
    [underlinetwo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-19);
        make.width.mas_equalTo((141-38));
        make.top.mas_equalTo(bltime.mas_bottom).offset(10);
        make.height.mas_equalTo(2.3);
    }];
    [self addbtview];
//    UIImageView *btview = [[UIImageView alloc] init];
//    btview.backgroundColor = [UIColor blackColor];
//    btview.userInteractionEnabled = YES;
//    self.btview = btview;
//    [self.view addSubview:btview];
//    [btview mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(30);
//        make.right.mas_equalTo(-30);
//        make.top.mas_equalTo(borderview.mas_bottom).offset(20);
//        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37-36-30);
//    }];
//    如果您希望自动布局在运行循环的下一轮更新布局，请调用 [view setNeedsLayout]。
//
//    但是，如果您希望它立即更新布局，以便您可以稍后在当前函数中立即访问新的边界值，或者在运行循环开始之前的另一个点，那么您需要调用 [view setNeedsLayout] 和 [view layoutIfNeeded]。
    [self.btview layoutIfNeeded];
    
#pragma mark 返回入口
    UIImageView *backview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"back"]];
    backview.userInteractionEnabled = YES;
    [self.view addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-37);
        make.width.mas_equalTo(33);
        make.height.mas_equalTo(36);
        make.left.mas_equalTo(26);
    }];
    
    UIButton *backbt = [UIButton buttonWithType:UIButtonTypeCustom];
    [backbt addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [backview addSubview:backbt];
    [backbt mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(backview);
    }];
    
    
    
    UIImageView *bluetherview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bluether"]];
    bluetherview.userInteractionEnabled = YES;
    [self.view addSubview:bluetherview];
    [bluetherview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.bottom.mas_equalTo(-SafeAreaBottomHeight-52);
        make.height.mas_equalTo(13);
    }];
    
    
    [self addform];
    
    UISlider *slider = [[UISlider alloc] init];
    slider.minimumValue = 0.0;
//    获取当前屏幕的亮度，赋值在滑块上
    CGFloat value = [UIScreen mainScreen].brightness;
    //03.当前值
    slider.value = value;
    [slider setMinimumTrackImage:[UIImage imageNamed:@"xuanzhong"] forState:UIControlStateNormal];
    [slider setMaximumTrackImage:[UIImage imageNamed:@"beijing"] forState:UIControlStateNormal];
    //    生成透明图片
    CGSize s=CGSizeMake(0.1, 0.1);
    UIGraphicsBeginImageContextWithOptions(s, 0, [UIScreen mainScreen].scale);
    UIRectFill(CGRectMake(0, 0, 0, 0));
    UIImage *img=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    //    设置按钮拇指图片
    //    添加图片，大按钮就去掉了，这个img置为空是去不掉的
    [slider setThumbImage:img forState:UIControlStateNormal];
    slider.transform = CGAffineTransformMakeRotation( 3 * M_PI_2);
    [slider setContinuous:YES];
    slider.hidden = YES;
    self.slider = slider;
    [self.view addSubview:slider];
    [slider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
    [slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(45);
        make.top.mas_equalTo(luminanceview.mas_bottom).offset(70);
        make.height.mas_equalTo(50);
        make.width.mas_equalTo(148);
    }];
    
    
    self.blueToothManager.ConnectionFailureBlock = ^(NSString * _Nonnull uuid) {
//        断开了链接
        NSLog(@"断开了链接111。。。。");
        NSLog(@"断开链接对应的uuid :%@",uuid);
        [weakSelf stopvibration];//暂停震动
        [[NSNotificationCenter defaultCenter]postNotificationName:@"bluetoothDisconnect" object:nil userInfo:@{@"uuid":uuid}];
        weakSelf.devicesignalview.image = [UIImage imageNamed:@"noSignal"];
        
        weakSelf.disconnectDeviceuuid = [NSString stringWithFormat:@"%@",uuid];
        [weakSelf.disconnectDeviceArray addObject:uuid];
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"disconnectDevice" object:nil userInfo:@{@"disconnectDevice":uuid}];
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"disconnectDevice" object:nil userInfo:nil];
        [weakSelf stopvibration];
        [weakSelf.blueToothManager StartScan];//打开蓝牙
        
        if(weakSelf.disconnectDeviceBlock){
//            weakSelf.disconnectDeviceBlock([NSString stringWithFormat:@"1%@",uuid]);
            weakSelf.disconnectDeviceBlock(@"1");
        }
        
    };
    
    self.blueToothManager.successfulConnectionBlock = ^(NSMutableArray * _Nonnull list) {
        NSLog(@"list %@",list);
        NSLog(@"链接成功 ");
        
        NSString* linkuuid = [NSString stringWithFormat:@"%@",list[1][@"uuid"]];
        if(weakSelf.disconnectDeviceBlock){
//            weakSelf.disconnectDeviceBlock([NSString stringWithFormat:@"2%@",linkuuid]);
            weakSelf.disconnectDeviceBlock(@"2");
        }
        [weakSelf.linkDeviceArray addObject:linkuuid];
        NSLog(@"linkuuid :%@",linkuuid);
//        [weakSelf resendTime];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
           // 在子线程中执行需要延时的代码
           [NSThread sleepForTimeInterval:1.0]; // 休眠0.8秒钟

           dispatch_async(dispatch_get_main_queue(), ^{
              // 回到主线程中执行剩余操作

               [weakSelf resendTime];
           });
        });
        [[NSNotificationCenter defaultCenter]postNotificationName:@"linkSuccess" object:nil userInfo:@{@"list":list}];
        
    };
    self.blueToothManager.bluetoothConnectionReturnBlock = ^{
        NSLog(@"蓝牙链接中");
        weakSelf.blueToothManager.isPopupStop = YES;
        
    };
    
    if(self.disconnectDeviceBlock){
        NSLog(@"disconnectDeviceArray :%ld %ld",self.disconnectDeviceArray.count ,self.linkDeviceArray.count);
        if(self.disconnectDeviceArray.count > self.linkDeviceArray.count){
            NSLog(@"返回来对应的uuid");
            self.disconnectDeviceBlock(self.disconnectDeviceuuid);
        }
        
    }
}

- (void)resendTime{
    
    //    存入蓝牙时间NSString *arr= [[NSUserDefaults standardUserDefaults] objectForKey:@"theme"];
    NSString *resendTime= [[NSUserDefaults standardUserDefaults] objectForKey:@"untraveledTime"];
    if(kStringIsEmpty(resendTime)||[resendTime isEqualToString:@"无"]){
        NSLog(@"显示无。");
        return;
    }
    NSLog(@"resendTime 1:%@",resendTime);
    
    self.ultimateTime = resendTime;
    self.timelb.text = [NSString stringWithFormat:@"%@",self.ultimateTime];
    //        发送蓝牙数据
    [self sendBluetoothData];
//    NSArray *array = [resendTime componentsSeparatedByString:@":"];
//    NSString * time = [NSString stringWithFormat:@"%@",array[0]];
//    NSString * hour = [NSString stringWithFormat:@"%@",array[1]];
//    int data = time.intValue;
//    int tow = hour.intValue;
//    NSLog(@"%d",data);
//    data = [XHbluetoolUtils intTransitionhex16:data];
//    if(data>12){
//        data = data - 12;
//    }
//
//    NSLog(@"12进制时间：%d",data);
//    tow = [XHbluetoolUtils intTransitionhex16:tow];
//    Byte send[] = {0x58,0x57,0x03,0xff,0x00,data,tow,0x01};
//    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
//    self.isArrive = NO;//发送时no 到指定位置折为yes
//    [self.blueToothManager writeData:sendData];
    
    [[NSUserDefaults standardUserDefaults] setObject:@"无" forKey:@"untraveledTime"];
    //这里建议同步存储到磁盘中，但是不是必须的
    [[NSUserDefaults standardUserDefaults] synchronize];
    
////        存入蓝牙时间
//    [[NSUserDefaults standardUserDefaults] setObject:resendTime forKey:@"Bluetoothtime"];
//    //这里建议同步存储到磁盘中，但是不是必须的
//    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)dianliang{
    
//    获取电量
    Byte send[] = {0x58,0x57,0x03,0x0B,0x00,0x00,0x00,0x01};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
    [self.blueToothManager writeData:sendData];
//    [self.blueToothManager writeDatatow:sendData uuid:self.UUID];
    //    链接
    WeakSelf
    self.blueToothManager.datastrBlock = ^(NSString * _Nonnull datastr) {
        NSLog(@"datastr:%@",datastr);
        NSArray *Arr = [NSArray array];
        Arr = [datastr componentsSeparatedByString:@"0x585703"];
        NSLog(@"componentsSeparatedByString========%@",Arr);
        NSString *results2 = [Arr[1] substringToIndex:2];
        NSLog(@"results2:%@",results2);
        int dinal = [XHbluetoolUtils hexadecimalto10:results2];
        NSLog(@"dinal :%d",dinal);
//        电量：0-100
        if(dinal >= 0&& dinal<=20){
            weakSelf.batterystatus.image = [UIImage imageNamed:@"batterystatus0"];
        }
        if(dinal >20 && dinal<=40){
            weakSelf.batterystatus.image = [UIImage imageNamed:@"batterystatus1"];
        }
        if(dinal >40 && dinal<=60){
            weakSelf.batterystatus.image = [UIImage imageNamed:@"batterystatus2"];
        }
        if(dinal >60 && dinal<=80){
            weakSelf.batterystatus.image = [UIImage imageNamed:@"batterystatus3"];
        }
        if(dinal >80 && dinal< 100){
            weakSelf.batterystatus.image = [UIImage imageNamed:@"batterystatus"];
        }
    };
}
-(void)addbtview{
    
    //    默认0.5滑动进度
    UIImageView *btview = [[UIImageView alloc] init];
    btview.backgroundColor = [UIColor blackColor];
    btview.userInteractionEnabled = YES;
    self.btview = btview;
    [self.view addSubview:btview];
    NSString *sliderarr= [[NSUserDefaults standardUserDefaults] objectForKey:@"luminance"];
    double num = [sliderarr doubleValue];
    
    if(winHeight<=736){
        
        if(num > 0){
            [self.btview mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo((30+90-(num-0.47)*90));
                make.right.mas_equalTo((-30-90+(num-0.47)*90));
                make.top.mas_equalTo(self.borderview.mas_bottom).offset(20);
        //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37-36-30-130+slider.value*100));
        //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37+slider.value*100));
                make.height.mas_equalTo((winWidth-160)/0.76+(num-0.47)*90);
                make.centerX.mas_equalTo(self.view);
            }];
        }else{
            [btview mas_makeConstraints:^(MASConstraintMaker *make) {

                make.left.mas_equalTo((30+90-40));
                make.right.mas_equalTo((-30-90+40));
                make.top.mas_equalTo(self.borderview.mas_bottom).offset(20);
        //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37-36-30-130+50));
        //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37+50));
                make.height.mas_equalTo((winWidth-160)/0.81);
            }];
        }
    }else{
        if(num > 0){
            [self.btview mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo((30+90-num*90));
                make.right.mas_equalTo((-30-90+num*90));
                make.top.mas_equalTo(self.borderview.mas_bottom).offset(20);
        //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37-36-30-130+slider.value*100));
        //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37+slider.value*100));
                make.height.mas_equalTo((winWidth-160)/0.76+num*90);
                make.centerX.mas_equalTo(self.view);
            }];
        }else{
            [btview mas_makeConstraints:^(MASConstraintMaker *make) {

                make.left.mas_equalTo((30+90-40));
                make.right.mas_equalTo((-30-90+40));
                make.top.mas_equalTo(self.borderview.mas_bottom).offset(20);
        //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37-36-30-130+50));
        //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37+50));
                make.height.mas_equalTo((winWidth-160)/0.81);
            }];
        }
        
    }
    
    
  /*  if(num > 0){
        [self.btview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo((30+90-num*90));
            make.right.mas_equalTo((-30-90+num*90));
            make.top.mas_equalTo(self.borderview.mas_bottom).offset(20);
    //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37-36-30-130+slider.value*100));
    //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37+slider.value*100));
            make.height.mas_equalTo((winWidth-160)/0.76+num*90);
            make.centerX.mas_equalTo(self.view);
        }];
    }else{
        [btview mas_makeConstraints:^(MASConstraintMaker *make) {

            make.left.mas_equalTo((30+90-40));
            make.right.mas_equalTo((-30-90+40));
            make.top.mas_equalTo(self.borderview.mas_bottom).offset(20);
    //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37-36-30-130+50));
    //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37+50));
            make.height.mas_equalTo((winWidth-160)/0.81);
        }];
    }
*/
}

-(void)scalingslider:(UISlider *)slider{
    NSLog(@"点击了缩放按钮%f %f",slider.value,winWidth);
   
    [self.btview removeFromSuperview];
    UIImageView *btview = [[UIImageView alloc] init];
    btview.backgroundColor = [UIColor blackColor];
    btview.userInteractionEnabled = YES;
    self.btview = btview;
    [self.view addSubview:btview];
    
    if(winHeight<=736){
        [self.btview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo((30+90-(slider.value-0.47)*90));
            make.right.mas_equalTo((-30-90+(slider.value-0.47)*90));
            make.top.mas_equalTo(self.borderview.mas_bottom).offset(20);
    //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37-36-30-130+slider.value*100));
    //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37+slider.value*100));
            make.height.mas_equalTo((winWidth-160)/0.76+(slider.value-0.47)*90-50);
            make.centerX.mas_equalTo(self.view);
        }];
    }else{
        [self.btview mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo((30+90-slider.value*90));
            make.right.mas_equalTo((-30-90+slider.value*90));
            make.top.mas_equalTo(self.borderview.mas_bottom).offset(20);
    //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37-36-30-130+slider.value*100));
    //        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37+slider.value*100));
            make.height.mas_equalTo((winWidth-160)/0.76+slider.value*90);
            make.centerX.mas_equalTo(self.view);
        }];
    }
   /* [self.btview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((30+90-slider.value*90));
        make.right.mas_equalTo((-30-90+slider.value*90));
        make.top.mas_equalTo(self.borderview.mas_bottom).offset(20);
//        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37-36-30-130+slider.value*100));
//        make.bottom.mas_equalTo((-SafeAreaBottomHeight-37+slider.value*100));
        make.height.mas_equalTo((winWidth-160)/0.76+slider.value*90);
        make.centerX.mas_equalTo(self.view);
    }];
    */
    
    [self.btview layoutIfNeeded];
    [self addform];
    NSString *sliderstr = [NSString stringWithFormat:@"%f",slider.value];
    //    存入
    [[NSUserDefaults standardUserDefaults] setObject:sliderstr forKey:@"luminance"];
      
      //这里建议同步存储到磁盘中，但是不是必须的
    [[NSUserDefaults standardUserDefaults] synchronize];
}
-(void)sliderValueChanged:(UISlider *)slider
{
    NSLog(@"slider value%f",slider.value);
    [[UIScreen mainScreen] setBrightness:slider.value];//亮度
}
-(void)back{
    
//    [self dismissViewControllerAnimated:YES completion:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)addform{
    
    

//    缩放大小逻辑：创view。然后用for in 遍历创建按钮，然后在slider进度条滑动的时候，删除添加按钮的view，再调用创建view和按钮。
//    在slider滑动代理的时候，根据滑动的数值来决定按钮的大小。 遍历删除view和按钮，在创建创建。
//    自定义宫格边
    
    NSArray *imagearry = @[@"helper1",@"helper2",@"helper3",@"helper4",@"helper5",@"helper6",@"helper7",@"helper8",@"helper9",@"helperx",@"helper0",@""];
    
    NSInteger tag = 100;
    
    for (NSInteger i = 0; i < 4; i ++) {
        for (NSInteger j = 0; j < 3; j ++) {
//            let heightInPoints = image.size.height
//            let heightInPixels = heightInPoints * image.scale

            
            CGFloat btnW = (self.btview.bounds.size.width/11)*3;
            CGFloat btnH = (self.btview.bounds.size.width/11)*3;
            CGFloat  btnX = j*btnW+j*(self.btview.bounds.size.width/11);
            CGFloat btnY = i*btnH+i*(self.btview.bounds.size.height/27);
            
//            CGFloat  btnX = j*btnW+j;
//            CGFloat btnY = i*btnH+i*13.6;
            
            UIImageView *numeview = [[UIImageView alloc] initWithFrame:CGRectMake(btnX, btnY, btnW, btnH)];
            numeview.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@",imagearry[tag-100]]];
            //            self.formview.userInteractionEnabled = YES;
            numeview.userInteractionEnabled = YES;
            [self.btview addSubview:numeview];
            
            UIButton *one = [UIButton buttonWithType:UIButtonTypeCustom];
            one.frame = CGRectMake(btnX, btnY, btnW, btnH);
            one.tag = tag ++;
            
            [self.btview addSubview:one];
            [one addTarget:self action:@selector(nuber:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    
    
    
}
- (void)newresettime{
    self.indexValue = 1;
    self.tenHour = 0;
//    self.bltime.text = @"00";
//    self.bltimetwo.text = @"00";
    self.bltime.text = @"  ";
    self.bltimetwo.text = @"  ";
    self.bltime.textColor = [UIColor whiteColor];
    self.bltimetwo.textColor = [UIColor whiteColor];
    self.maohaolb.textColor = [UIColor whiteColor];
}
- (void)resettime{
    
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       // 在子线程中执行需要延时的代码
       [NSThread sleepForTimeInterval:0.7]; // 休眠0.8秒钟

       dispatch_async(dispatch_get_main_queue(), ^{
          // 回到主线程中执行剩余操作
           self.indexValue = 1;
           self.tenHour = 0;
       //    self.bltime.text = @"00";
       //    self.bltimetwo.text = @"00";
           self.bltime.text = @"  ";
           self.bltimetwo.text = @"  ";
           self.bltime.textColor = [UIColor whiteColor];
           self.bltimetwo.textColor = [UIColor whiteColor];
           self.maohaolb.textColor = [UIColor whiteColor];
       });
    });
//    self.indexValue = 1;
//    self.tenHour = 0;
////    self.bltime.text = @"00";
////    self.bltimetwo.text = @"00";
//    self.bltime.text = @"  ";
//    self.bltimetwo.text = @"  ";
//    self.bltime.textColor = [UIColor whiteColor];
//    self.bltimetwo.textColor = [UIColor whiteColor];
//    self.maohaolb.textColor = [UIColor whiteColor];
}

-(void)nuber:(UIButton *)button{
    NSLog(@"点击了：%ld",button.tag);
//    点击按键时候隐藏亮度和缩放
    self.scalingslider.hidden = YES;
    self.slider.hidden = YES;
    
    if(button.tag == 109){
        [self newresettime];
        return;
    }
    if(button.tag == 111){
        NSLog(@"点击了最后一个");
        return;
    }
    //时间24小时制
    
    if(self.indexValue == 1){//第一次按钮
        //如果第一次点击，100 = 1  101 = 2  110 = 0 对应的按钮 那就是第一个位置
//        第一次点击了0 1 2 那就是24小时制第一个位置。
        if(button.tag == 100||button.tag == 101||button.tag == 110){
            NSString *time;
            if(button.tag == 110){//点击了0
                self.tenHour = 0;
                time = [self.bltime.text stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"0"];
                NSLog(@"time :%@",time);
            }else{
                time = [self.bltime.text stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[NSString stringWithFormat:@"%ld",button.tag-99]];
                self.tenHour = button.tag-99;
                NSLog(@"time :%@",time);
            }
            NSMutableAttributedString *mutAttStr = [[NSMutableAttributedString alloc]initWithString:time];
            [mutAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0,1)]; //根据具体字符串的内容来设置具体部分的颜色
            self.bltime.attributedText = mutAttStr;
//            self.bltime.text = time;
            self.indexValue = 2;//第一个没问题
            return;
            
        }else{
            NSString *time;
            //24小时制，如果第一个点击不是0 1 2  第一位那就是 0；
            time = [self.bltime.text stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"0"];
            self.bltime.text = [time stringByReplacingCharactersInRange:NSMakeRange(1, 1) withString:[NSString stringWithFormat:@"%ld",button.tag-99]];
            NSMutableAttributedString *mutAttStr = [[NSMutableAttributedString alloc]initWithString:self.bltime.text];
            [mutAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0,2)]; //根据具体字符串的内容来设置具体部分的颜色
            self.bltime.attributedText = mutAttStr;
            
            self.indexValue = 3;//第一个
            self.tenHour = 0;
            return;
        }
        
    }
//    第二次点击 已经知道第一个是0 1 2
    if(self.indexValue == 2){
        NSLog(@"self.tenHour :%ld",self.tenHour);
//        如果第一个十位 是0 1 个位0-9都可以
        if(self.tenHour ==0 ||self.tenHour == 1){
            if(button.tag == 110){
                self.bltime.text = [self.bltime.text stringByReplacingCharactersInRange:NSMakeRange(1, 1) withString:@"0"];
            }else{
                self.bltime.text = [self.bltime.text stringByReplacingCharactersInRange:NSMakeRange(1, 1) withString:[NSString stringWithFormat:@"%ld",button.tag-99]];
                
            }
            NSMutableAttributedString *mutAttStr = [[NSMutableAttributedString alloc]initWithString:self.bltime.text];
            [mutAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0,2)]; //根据具体字符串的内容来设置具体部分的颜色
            self.bltime.attributedText = mutAttStr;
            self.indexValue = 3;
            self.unitHour = button.tag-99;
            return;
        }
        
        if(self.tenHour == 2){
            if(button.tag == 110 || button.tag == 100 || button.tag == 101|| button.tag == 102){
                NSString *time;
                if(button.tag == 110){//点击了0
                    self.unitHour = 0;
                    time = [self.bltime.text stringByReplacingCharactersInRange:NSMakeRange(1, 1) withString:@"0"];
                    NSLog(@"time :%@",time);
                }else{
                    time = [self.bltime.text stringByReplacingCharactersInRange:NSMakeRange(1, 1) withString:[NSString stringWithFormat:@"%ld",button.tag-99]];
                    self.unitHour = button.tag-99;
                    NSLog(@"time :%@",time);
                }
                self.bltime.text = time;
                NSMutableAttributedString *mutAttStr = [[NSMutableAttributedString alloc]initWithString:self.bltime.text];
                [mutAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0,2)]; //根据具体字符串的内容来设置具体部分的颜色
                self.bltime.attributedText = mutAttStr;
                
                self.indexValue = 3;
                return;
            }else{
                NSLog(@"小时十位是2 个位不能是%ld",button.tag-99);
            }
        }
       
    }
    
    if(self.indexValue == 3){//第三个数字
        NSLog(@"第三个数字 十分位置是：%ld",button.tag-99);
//        十分位tenMinute  最多
        if(button.tag == 100||button.tag == 101||button.tag == 102||button.tag == 103||button.tag == 104||button.tag == 110){
            NSString *time;
            if(button.tag == 110){//点击了0
                
                time = [self.bltimetwo.text stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"0"];
                
            }else{
                time = [self.bltimetwo.text stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:[NSString stringWithFormat:@"%ld",button.tag-99]];
                
            }
            self.bltimetwo.text = time;
            self.maohaolb.textColor = [UIColor greenColor];
            NSMutableAttributedString *mutAttStr = [[NSMutableAttributedString alloc]initWithString:self.bltimetwo.text];
            [mutAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0,1)]; //根据具体字符串的内容来设置具体部分的颜色
            self.bltimetwo.attributedText = mutAttStr;
            
            self.tenMinute = button.tag-99;
            self.indexValue = 4;
            return;
            NSLog(@"time :%@",time);
        }else{
            NSLog(@"十分位不能是：%ld",button.tag-99);
        }
        
    }
    if(self.indexValue == 4){
        
        NSString *time;
        if(button.tag == 110){//点击了0
            
            time = [self.bltimetwo.text stringByReplacingCharactersInRange:NSMakeRange(1, 1) withString:@"0"];
            
        }else{
            time = [self.bltimetwo.text stringByReplacingCharactersInRange:NSMakeRange(1, 1) withString:[NSString stringWithFormat:@"%ld",button.tag-99]];
        }
            NSLog(@"time :%@",time);
            self.bltimetwo.text = time;
        NSMutableAttributedString *mutAttStr = [[NSMutableAttributedString alloc]initWithString:self.bltimetwo.text];
        [mutAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0,2)]; //根据具体字符串的内容来设置具体部分的颜色
        self.bltimetwo.attributedText = mutAttStr;
        
            self.indexValue = 5;
            self.ultimateTime = [NSString stringWithFormat:@"%@:%@",self.bltime.text,self.bltimetwo.text];
        
//        发送蓝牙数据
            [self sendBluetoothData];
            return;
    }
    
    
    
}
-(void)sendBluetoothData{
    
//    NSLog(@"发送蓝牙数据：%@",self.ultimateTime);
//    NSData *datastr = [self.ultimateTime dataUsingEncoding:NSUTF8StringEncoding];
//    [self.blueToothManager writeData:datastr];
    
    NSArray *array = [self.ultimateTime componentsSeparatedByString:@":"];
    NSString * time = [NSString stringWithFormat:@"%@",array[0]];
    NSString * hour = [NSString stringWithFormat:@"%@",array[1]];
    int data = time.intValue;
    int tow = hour.intValue;
    NSLog(@"%d",data);
    data = [XHbluetoolUtils intTransitionhex16:data];
    if(data>12){
        data = data - 12;
    }
    
    NSLog(@"12进制时间：%d",data);
    tow = [XHbluetoolUtils intTransitionhex16:tow];
    Byte send[] = {0x58,0x57,0x03,0xff,0x00,data,tow,0x01};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
    
    self.isArrive = NO;//发送时no 到指定位置折为yes
    NSLog(@"writeCharcteristic :%@",self.blueToothManager.writeCharcteristic);
    NSLog(@"connectPeripheral :%@",self.blueToothManager.connectPeripheral);
    
    
#pragma mark 发送蓝牙数据
    [self.blueToothManager writeData:sendData];
    
//    [self.blueToothManager writeDatatow:sendData uuid:self.UUID];
    
    
    if(data<10){
        self.ultimateTime = [NSString stringWithFormat:@"0%d:%@",data,hour];
    }else{
        self.ultimateTime = [NSString stringWithFormat:@"%d:%@",data,hour];
    }
     
    
   
    
//    震动
//    if (@available(iOS 10.0, *)) {
//        UINotificationFeedbackGenerator *generator = [[UINotificationFeedbackGenerator alloc] init];
//        [generator notificationOccurred:UINotificationFeedbackTypeWarning];
//    }
//    if(@available(iOS 10.0, *)){
//            UIImpactFeedbackGenerator *impactFeedBack = [[UIImpactFeedbackGenerator alloc] initWithStyle: UIImpactFeedbackStyleMedium];
//            [impactFeedBack prepare];
//            [impactFeedBack impactOccurred];
//        }
    
    
    self.timelb.text = self.ultimateTime;
    [self resettime];//重置时间
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
       // 在子线程中执行需要延时的代码
//        AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, nil);
       [NSThread sleepForTimeInterval:0.5]; // 休眠0.8秒钟

       dispatch_async(dispatch_get_main_queue(), ^{
          // 回到主线程中执行剩余操作
           
           [self getdataobtainrotationalstate];
       });
    });
    
//    [self getdataobtainrotationalstate];
}
- (void)getdataobtainrotationalstate{
    
    Byte send[] = {0x58,0x57,0x03,0x0B,0x0,0x00,0x00,0x03};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
    [self.blueToothManager writeData:sendData];
//    [self.blueToothManager writeDatatow:sendData uuid:self.UUID];
    [self.vibrationTimer invalidate];
    if(self.blueToothManager.connectionState){
        NSLog(@"链接状态");
       
    }else{
        NSLog(@"未链接状态");
        NSLog(@"%@",self.ultimateTime);
        NSLog(@"resendTime 2:%@",self.ultimateTime);
        //    存入蓝牙时间
        [[NSUserDefaults standardUserDefaults] setObject:self.ultimateTime forKey:@"untraveledTime"];
        //这里建议同步存储到磁盘中，但是不是必须的
        [[NSUserDefaults standardUserDefaults] synchronize];
        return;
    }
    if(self.blueToothManager.bluetoothDiscoverable){
        NSLog(@"bluetoothDiscoverable链接状态");
        
    }else{
        NSLog(@"bluetoothDiscoverable 未链接状态");
        NSLog(@"%@",self.ultimateTime);
        NSLog(@"resendTime 2:%@",self.ultimateTime);
        //    存入蓝牙时间
        [[NSUserDefaults standardUserDefaults] setObject:self.ultimateTime forKey:@"untraveledTime"];
        //这里建议同步存储到磁盘中，但是不是必须的
        [[NSUserDefaults standardUserDefaults] synchronize];
        return;
    }
    
    self.finishTime = self.ultimateTime;//保留跑完成的时间
    if (self.bluetoothtimeBlock) {
//        self.bluetoothtimeBlock(self.ultimateTime);
        self.bluetoothtimeBlock(self.finishTime);
    }
    //    存入蓝牙时间
    [[NSUserDefaults standardUserDefaults] setObject:self.ultimateTime forKey:@"Bluetoothtime"];
    //这里建议同步存储到磁盘中，但是不是必须的
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    NSLog(@"到震动提示");
   
    AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, nil);
    
    self.vibrationTimer = [NSTimer scheduledTimerWithTimeInterval:0.08 target:self selector:@selector(playkSystemSound) userInfo:nil repeats:YES];

    
}
- (void)playkSystemSound{
    NSLog(@"开始震动中 -- ");
    AudioServicesPlaySystemSoundWithCompletion(kSystemSoundID_Vibrate, nil);
//    获取现在是否走的，返回02就是走到正常的时间
    Byte send[] = {0x58,0x57,0x03,0x0B,0x0,0x00,0x00,0x08};
    NSData *sendData = [NSData dataWithBytes:&send length:sizeof(send)];
//    [self.blueToothManager writeData:sendData];
    [[BlueToothManager shareInstance] writeData:sendData];
//    [self.blueToothManager writeDatatow:sendData uuid:self.UUID];
    
    
}

- (void)stopvibration{
    
    NSLog(@"暂停震动");
    [self.vibrationTimer invalidate];
}
-(void)scalingbt{
    
    NSLog(@"缩放按钮");
    if(self.scalingslider.hidden){
        self.scalingslider.hidden = NO;
    }else{
        self.scalingslider.hidden = YES;
    }
}
-(void)luminancebt{
    
    NSLog(@"亮度按钮");
    
    if(self.slider.hidden){
        self.slider.hidden = NO;
    }else{
        self.slider.hidden = YES;
    }
    
}
- (void)dealloc{

    if(self.isArrive){
        NSLog(@"跑到对应的时间");
        
    }else{
        NSLog(@"没跑到对应的时间");
        NSLog(@"resendTime 3:%@",self.ultimateTime);
        //    存入蓝牙时间
//        [[NSUserDefaults standardUserDefaults] setObject:self.ultimateTime forKey:@"untraveledTime"];
//        //这里建议同步存储到磁盘中，但是不是必须的
//        [[NSUserDefaults standardUserDefaults] synchronize];
        
    }
    [self stopvibration];
    
//    if(self.disconnectDeviceBlock){
//        NSLog(@"disconnectDeviceArray :%ld %ld",self.disconnectDeviceArray.count ,self.linkDeviceArray.count);
//        if(self.disconnectDeviceArray.count > self.linkDeviceArray.count){
//            NSLog(@"返回来对应的uuid");
//            self.disconnectDeviceBlock(self.disconnectDeviceuuid);
//        }
//
//    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
